/**
MongoAspJ.aj
jackysoft-basic-core
2012-7-25上午10:03:34
@author 屈甲康
@QQ285799123
mailto:qujiakang@126.com
**/
package org.jackysoft.aop;

import java.util.Collection;
import java.util.Date;

import org.jackysoft.entity.nosql.AbstractNoSqlEntity;

import com.mongodb.DBObject;
public aspect MongoAspJ {
   pointcut setField(Object val):execution(void org.jackysoft.entity.nosql.*.set*(*))
   && this(AbstractNoSqlEntity)  
   && args(val);
   
   pointcut getField():
	   execution(String org.jackysoft.entity.nosql.*.get*())
	|| execution(Object org.jackysoft.entity.nosql.*.get*())
	|| execution(boolean org.jackysoft.entity.nosql.*.is*())
	|| execution(byte org.jackysoft.entity.nosql.*.get*())
	|| execution(short org.jackysoft.entity.nosql.*.get*())
	|| execution(int org.jackysoft.entity.nosql.*.get*())
	|| execution(float org.jackysoft.entity.nosql.*.get*())
	|| execution(long org.jackysoft.entity.nosql.*.get*())
	|| execution(double org.jackysoft.entity.nosql.*.get*())
	|| execution(Boolean org.jackysoft.entity.nosql.*.is*())
	|| execution(Byte org.jackysoft.entity.nosql.*.get*())
	|| execution(Short org.jackysoft.entity.nosql.*.get*())
	|| execution(Integer org.jackysoft.entity.nosql.*.get*())
	|| execution(Float org.jackysoft.entity.nosql.*.get*())
	|| execution(Long org.jackysoft.entity.nosql.*.get*())
	|| execution(Double org.jackysoft.entity.nosql.*.get*())
	|| execution(Date org.jackysoft.entity.nosql.*.get*())
    || execution(Collection<String> org.jackysoft.entity.nosql.*.get*())
    || execution(Collection<? extends AbstractNoSqlEntity> org.jackysoft.entity.nosql.*.get*())
    
    && this(AbstractNoSqlEntity)
    && within(org.jackysoft.entity.nosql.*);
   
   void around(Object val):setField(val){
	   AbstractNoSqlEntity ane = (AbstractNoSqlEntity)thisJoinPoint.getTarget();
	   String name = thisJoinPoint.getSignature().getName();
	   char first = name.charAt(3);
	   String fieldName = Character.toLowerCase(first)+name.substring("set".length()+1);
	   DBObject dbo = ane.getRoot();
	   if(val instanceof Enum){
		   Enum<?> e = (Enum<?>) val;
		   dbo.put(fieldName, e.ordinal());
	   }else{
		   dbo.put(fieldName, val);
	   }
	   
	   proceed(val);
	   
   }
      
   Object around():getField(){
	   return proceed();
   }
}
